// 推荐歌单获取
import { Playlist, PlaylistList } from '../models/DiscoverData';
import { requestGet } from '../utils/httpUtil';
import { promptAction } from '@kit.ArkUI';

export async function getNewPlaylist(
  newPlaylistImageUrl: Array<string>,
  newPlaylistName: Array<string>,
  result: string
): Promise<{ imageUrls: Array<string>, playlistNames: Array<string> } | string> {
  let NewPlaylist: PlaylistList = new PlaylistList();

  try {
    const response = await requestGet('/personalized');
    NewPlaylist = JSON.parse(response);
    newPlaylistImageUrl = [];
    newPlaylistName = [];
    if (NewPlaylist && Array.isArray(NewPlaylist.playlist)) {
      NewPlaylist.playlist.forEach((playlist: Playlist) => {
        if (playlist.id) {
          newPlaylistImageUrl.push(playlist.picUrl); // 将专辑封面添加到数组
          newPlaylistName.push(playlist.name); // 歌曲名称添加到数组
        }
      });
    }
    return {
      imageUrls: newPlaylistImageUrl,
      playlistNames: newPlaylistName,
    };
  } catch (err) {
    result = '错误代码为：' + err.code + ', 错误信息：' + JSON.stringify(err);
    promptAction.showToast({ message: result }); // 弹出错误提示
    console.error(result);
    return result;
  }
}
